<?php
/*
    Authors and license
*/

/* Messages table in SQL contains following values:
	Id, From, To, Date, Title, Text, Read
*/

/*	Grabbing All incoming message of the user.
*/	
function printUserInbox($id) { // you must specify user Id

	//Firstly lets print header of message table
	echo '';

	$result = mysql_query("SELECT * FROM `Messages` WHERE `To`='$id' ORDER BY Date DESC")
		or die("Error in database!"); 
		
	echo '<table id="noBorderTableRowed">';
	
	while($row = mysql_fetch_array($result)) { // grab row after row from result array
		
		/* including correct image */
		if($row['Read']==0){
			$img = '<img src="files/message/img/unread32.png">';
		} else {
			$img = '<img src="files/message/img/read32.png">';
		}
		
		/* changing color when over */
		?>		
		<tr onclick="document.location = '<?php echo '?mod=message&action=view&message='.$row['Id']; ?>';" onmouseover="this.className='bgSelected';" onmouseout="this.className='bgTransparent';" style="cursor: pointer;">
		<?php
		echo '<td>'.$img.'</td>';
		echo '<td style="width:100%">'.$row['Title'].'</td>';
		echo '<td>'.grabUserLoginFromId($row['From']).'</td>';
		echo '<td>'.date("d/m/y G:i", $row['Date']).'</td>';
		echo '</a></tr>';
		
		}
		
	echo '</table>';
} // end of printUserInbox()

/*	Grabbing All outcoming message of the user.
*/	
function printUserOutbox($id) { // you must specify user Id

	//Firstly lets print header of message table
	echo '';

	$result = mysql_query("SELECT * FROM `Messages` WHERE `From`='$id' ORDER BY Date DESC")
		or die("Error in database!"); 
		
	echo '<table id="noBorderTableRowed">';
	
	while($row = mysql_fetch_array($result)) { // grab row after row from result array
		
		/* including correct image */
		if($row['Read']==0){
			$img = '<img src="files/message/img/unread32.png">';
		} else {
			$img = '<img src="files/message/img/read32.png">';
		}
		
		/* changing color when over */
		?>		
		<tr onclick="document.location = '<?php echo '?mod=message&action=view&message='.$row['Id']; ?>';" onmouseover="this.className='bgSelected';" onmouseout="this.className='bgTransparent';" style="cursor: pointer;">
		<?php
		echo '<td>'.$img.'</td>';
		echo '<td style="width:100%">'.$row['Title'].'</td>';
		echo '<td>'.grabUserLoginFromId($row['To']).'</td>';
		echo '<td>'.date("d/m/y G:i", $row['Date']).'</td>';
		echo '</a></tr>';
		
		}
		
	echo '</table>';
} // end of printUserOutbox()

/*	Sending message.
	We need From, To, Title and Text
*/
function addNewMessage($from, $to, $title, $text) {	
	$now = time();

	mysql_query("INSERT INTO `Messages` (`Id`, `From`, `To`, `Date`, `Title`, `Text`, `Read`) VALUES (NULL, '$from', '$to', '$now', '$title', '$text', '0')");
} // end of addNewMessage()

/* Select User Id for givven Login
   Return 0 if there is no such user
*/
function grabUserIdFromLogin($name) {
	$result = mysql_query("SELECT `Id` FROM `Users` WHERE `Login`='$name'")
		or die("Error in database!");
		
	$row = mysql_fetch_array($result);
		if($row['Id']!="") $uid = $row['Id'];
		else $uid = 0;
		
	return $uid;
} // end of grabUserIdFromLogin()

/* Select User Name for givven Id
   Return "" if there is no such user
*/
function grabUserLoginFromId($id) {
	$result = mysql_query("SELECT `Login` FROM `Users` WHERE `Id`='$id'")
		or die("Error in database!");
		
	$row = mysql_fetch_array($result);
		if($row['Login']!="") $uname = $row['Login'];
		else $uname = 'No such user (ERROR)';
		
	return $uname;
} // end of grabUserLoginFromId()

?>